const { merge } = require( 'webpack-merge' )
const commonConfig = require( './webpack.common.js' )
const { DefinePlugin } = require( 'webpack' )
const MiniCssExtractPlugin = require( 'mini-css-extract-plugin' )//压缩css
const { CleanWebpackPlugin } = require( "clean-webpack-plugin" );
const HtmlWebpackPlugin = require( "html-webpack-plugin" );
const OptimizeCSSAssetsPlugin = require( "optimize-css-assets-webpack-plugin" );
const TerserPlugin = require( "terser-webpack-plugin" );

const path = require( 'path' )
module.exports = merge( commonConfig, {
    mode: 'production',
   
    // devtool: "none",
    output: {
        filename: "js/[name].[hash:8].js",
         publicPath: "./"
    },
    module: {
        rules: [
            {
                test: /\.less$/,
                use: [MiniCssExtractPlugin.loader,'css-loader', 'less-loader']
            },
        ]
    },
    plugins: [
        new CleanWebpackPlugin(),
        new HtmlWebpackPlugin( {
            title: 'Vue Webpack Sample',
            meta: {
                viewport: 'width=device-width'
            },
            template: "./public/index.html",
            favicon: "./public/favicon.ico", // 在此处设置
            filename: 'index-demo.html',
            chunks:['index'],
            //对html代码进行压缩
            minify: {
                removeComments: true, //去注释
                collapseWhitespace: true, //压缩空格
                removeAttributeQuotes: true //去除属性引用
            }
        } ),
       
        new DefinePlugin( {
            BASE_URL: process.env.NODE_ENV
        } ),
        new MiniCssExtractPlugin( {
            filename: 'css/[name].[hash:8].css',
        } ),
    ],
    optimization:{
        splitChunks: {
            chunks: "all"
        },
        minimize: true,
        minimizer: [
            //css压缩
            new OptimizeCSSAssetsPlugin( {
                cssProcessorOptions: {
                    discardComments: { remove: true } //移除注释
                }
            } ),
            new TerserPlugin( {
                parallel: true, //开启多线程来提高构建速度
                terserOptions: {
                    warnings: false, //不展示warning
                    compress: {
                        unused: true, //去除未使用的
                        drop_debugger: true, //移除debugger
                        drop_console: true //去除console
                    },
                    output: {
                        comments: false //去除注释
                    }
                }
            } )
        ]
    }
} )